package com.example.myapplication10.Activities;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication10.Buttons.Btn_smallAQI;
import com.example.myapplication10.DTO.ForecastDTO;
import com.example.myapplication10.R;

import java.util.ArrayList;
import java.util.List;


public class FcstRecycleAdapter extends RecyclerView.Adapter<FcstRecycleAdapter.VH> {
    private boolean delFlag = false;
    private boolean prevDelFlag = false;

    public boolean isDelFlag() {
        return delFlag;
    }

    public void setDelFlag(boolean delFlag) {
        this.delFlag = delFlag;
    }

    private List<ForecastDTO> forecastDTOList;
    private List<ForecastDTO> newForecastDTOList;
    private WeatherImg weatherImg = new WeatherImg();

    public List<ForecastDTO> getForecastDTOList() {
        return forecastDTOList;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<ForecastDTO> datas) {
        this.forecastDTOList = datas;
        this.newForecastDTOList = new ArrayList<>();
    }

    public FcstRecycleAdapter(){
        this.forecastDTOList = new ArrayList<>();
        this.newForecastDTOList = new ArrayList<>();
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.forecast_data,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, @SuppressLint("RecyclerView") int position) {
        holder.forecastType.setText(forecastDTOList.get(position).getType());
        holder.forecastType.setCompoundDrawablesWithIntrinsicBounds
                (0,weatherImg.getWeatherImg(forecastDTOList.get(position).getType()),0,0);
        holder.min.setText(forecastDTOList.get(position).getLow()
                .replace("低温 ","").replace("℃","°"));
        holder.max.setText(forecastDTOList.get(position).getHigh()
                .replace("高温 ","").replace("℃","°"));
        holder.windLi.setText(forecastDTOList.get(position).getFl());
        holder.date.setText(forecastDTOList.get(position).getDate());
        holder.weekday.setText(forecastDTOList.get(position).getWeek());
        holder.btn_smallAQI.update(forecastDTOList.get(position).getAqi());
    }

//        holder.forecastType.setText(forecastDTOList.get(position).getType());
//        holder.forecastType.setText(forecastDTOList.get(position).getType());
//        holder.forecastType.setText(forecastDTOList.get(position).getType());
//        String txt = String.valueOf(position+1);
//        holder.index.setText(txt);
//        txt = (datas.get(position).get("城市名称"));
//        holder.city0.setText(txt);
//        txt = (datas.get(position).get("城市代码"));
//        holder.code0.setText(txt);
//        if (isDelFlag()){
//            prevDelFlag = true;
//            holder.btn_add0.setVisibility(View.GONE);
//            holder.btn_del0.setVisibility(View.VISIBLE);
//            holder.btn_del0.setOnClickListener(new View.OnClickListener() {
//                @SuppressLint("NotifyDataSetChanged")
//                @Override
//                public void onClick(View v) {
//                    newDatas.add(datas.get(position));
//                    datas.remove(position);
//                    FcstRecycleAdapter.this.notifyDataSetChanged();
//                    EventBus.getDefault().post(new MsgEventRemoveCity(newDatas));
//                }
//            });
//        }
//        else {
//            prevDelFlag = false;
//            holder.btn_add0.setVisibility(View.VISIBLE);
//            holder.btn_del0.setVisibility(View.GONE);
//            holder.btn_add0.setOnClickListener(new View.OnClickListener() {
//                @SuppressLint("NotifyDataSetChanged")
//                @Override
//                public void onClick(View v) {
//                    newDatas.add(datas.get(position));
//                    datas.remove(position);
//                    FcstRecycleAdapter.this.notifyDataSetChanged();
//                    EventBus.getDefault().post(new MsgEventAddCity(newDatas));
//                }
//            });
//        }

    @Override
    public int getItemCount() {
        return forecastDTOList.size();
    }

    public static class VH extends RecyclerView.ViewHolder{
        TextView weekday,date,windLi;
        TextView max,min,forecastType;
        Btn_smallAQI btn_smallAQI;
        public VH(@NonNull View itemView) {
            super(itemView);
            btn_smallAQI = itemView.findViewById(R.id.aqi);
            weekday = itemView.findViewById(R.id.weekday);
            date = itemView.findViewById(R.id.date);
            windLi = itemView.findViewById(R.id.wind);
            max = itemView.findViewById(R.id.max);
            min = itemView.findViewById(R.id.min);
            forecastType = itemView.findViewById(R.id.type_fcst);
        }
    }
}


